class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        sort(nums.begin(), nums.end());
        vector<vector<int>> res;
        for (int i=0; i<nums.size(); i++){
            if (nums[i] > target && nums[i] >= 0) break; // 一级剪枝
            if (i > 0 && nums[i] == nums[i - 1]) continue;

            for (int j = i + 1; j<nums.size(); j++){
                if (nums[i] + nums[j] > target && nums[j] >= 0) break;// 二级剪枝
                if (j > i + 1 && nums[j] == nums[j - 1]) continue;

                int left = j + 1;
                int right = nums.size() - 1;

                while(left < right){
                    if ((long)nums[i] + nums[j] + nums[left] + nums[right] > target) right--; //防止溢出int型的最大值，使用long
                    else if ((long)nums[i] + nums[j] + nums[left] + nums[right] < target) left++;
                    else {
                        res.push_back({nums[i], nums[j], nums[left], nums[right]});
                        while (left < right && nums[left] == nums[left + 1]) left++;
                        while (left < right && nums[right] == nums[right - 1]) right--;
                        left++;
                        right--;
                    }
                }
            }
        }
        return res;
    }
};